/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mavenrentcars.service.impl;

import com.mavenrentcars.dao.CarDao;
import com.mavenrentcars.exception.ViewException;
import com.mavenrentcars.form.CarForm;
import com.mavenrentcars.model.Car;
import com.mavenrentcars.model.Place;
import com.mavenrentcars.model.User;
import com.mavenrentcars.service.AccountService;
import com.mavenrentcars.service.CarService;
import com.mavenrentcars.service.PlaceService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 *
 * @author serg
 */
@Service("carService")
public class CarServiceImpl implements CarService {

    @Resource(name = "carDao")
    CarDao carDao;
    @Resource(name = "accountService")
    AccountService accountService;
    @Resource(name = "placeService")
    PlaceService placeService;

    @Override
    public Car save(Car model) {
        return carDao.save(model);
    }

    @Override
    public Car get(Integer id) {
        return carDao.get(id);
    }

    @Override
    public void remove(Integer id) {
        carDao.remove(id);
    }

    @Override
    public List<Car> getAll() {
        return carDao.getAll();
    }

    @Override
    public List<Car> getAll(Date start_date, Date end_date) {
        return carDao.getAll(start_date, end_date);
    }

    @Override
    public List<Car> getAll(Place start_place) {
        return carDao.getAll(start_place);
    }

    @Override
    public List<Car> getAll(Place start_place, Date start_date, Date end_date) {
        return carDao.getAll(start_place, start_date, end_date);
    }

    @Override
    public List<Car> getAllByUser() {
        User account = accountService.getLoggedInAccount();
        return carDao.getAll(account);
    }

    @Override
    public Car add(CarForm form) throws ViewException {

        Car item = new Car();
        item.setModel(form.getModel());
        item.setOutputYear(Integer.parseInt(form.getOutputYear()));
        item.setPrice(new BigDecimal(form.getPrice()));
        item.setInsurance(form.getInsurance());
        item.setUser(accountService.getLoggedInAccount());
        item.setStartPlace(placeService.get(Integer.parseInt(form.getStartPlace())));

        Car car = carDao.save(item);
        setPlaces(car, form.getPlaces());

        return car;
    }

    public void setPlaces(Car car, Map<String, String> pmap) {
        try {
            Set<Place> places = new HashSet<Place>();
            for (Map.Entry<String, String> entry : pmap.entrySet()) {
                String key = entry.getKey();
                //lnk place to car
                Place place = placeService.get(Integer.parseInt(key));
                places.add(place);
            }

            car.setPlaces(places);
            carDao.save(car);
        } catch (ViewException ex) {
            Logger.getLogger(AccountServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Car edit(CarForm form) throws ViewException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
